<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>垃圾桶</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 400px;
        width: 700px;
        background-color: #cacaca;
        font-size: 40px;
        text-align: center;
      }

      .trashCan {
        width: 30%;
        height: 100%;
        background-color: rgb(173, 173, 173);
      }

      .list {
        font-size: 20px;
        width: 60%;
        height: 100%;
        background-color: rgb(234, 234, 234);
        p {
          height: 16%;
          border: 1px dashed #999;
          line-height: 300%;
          background-color: rgb(232, 223, 208);
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="trashCan">垃圾桶</div>
      <div class="list">
        <p draggable="true" id="p1">1</p>
        <p draggable="true" id="p2">2</p>
        <p draggable="true" id="p3">3</p>
        <p draggable="true" id="p4">4</p>
        <p draggable="true" id="p5">5</p>
        <p draggable="true" id="p6">6</p>
      </div>
    </div>
    <script>
      var trashCan = document.querySelector(".trashCan");
      var list = document.querySelector(".list");
      var pList = document.querySelectorAll(".list p");
      list.addEventListener("dragstart", function (e) {
        console.log(e.target.id);
        e.dataTransfer.setData("dataId", e.target.id);
      });
      trashCan.addEventListener("dragover", function (e) {
        e.preventDefault();
      });
      trashCan.addEventListener("drop", function (e) {
        // e.preventDefault();
        var dataId = e.dataTransfer.getData("dataId");
        // 根据dataId获取元素，并将其从文档中移除
        document.getElementById(dataId).remove();
      });
    </script>
  </body>
</html>
